<?php

define('MOSCOW_BRANCH_ID', 1);


class farm_products {
	
	//------------------------------------------------------------------------
	function get_products_by_ids($products_ids, $branch_id) {
		
		$products = array();
		
		$auth_user = isset($GLOBALS["TSFE"]->fe_user->user["uid"]);
		
		if ($auth_user) {
			$uid = $GLOBALS["TSFE"]->fe_user->user["uid"];
			
			$user['branch_id'] = $GLOBALS["TSFE"]->fe_user->user["user_farm_account_branch_id"];
			$user['opt'] = $GLOBALS["TSFE"]->fe_user->user["user_farm_account_opt"];
		}
		
		if (!isset($branch_id) || ($branch_id == 0)) {
			$branch_id = $user['branch_id'];
		}
		
		$products_ids_sql = implode(',', $products_ids);
		
		$enable_sql = tslib_cObj::enableFields('tx_commerce_products');
		
		
		$sql = "
			SELECT		tx_commerce_products.uid AS product_id,
						tx_commerce_products.title,
						tx_commerce_products.manufacturer_uid,
						tx_commerce_products.user_farm_commerce_by_request AS by_request,
						
						product_branch_mm.branch_price,
						product_branch_mm.branch_price_opt,
						product_branch_mm.rest,
						
						user_farm_commerce_discount_values.discount
			
			FROM		tx_commerce_products  LEFT JOIN user_farm_commerce_product_branch_mm AS product_branch_mm ON
								((tx_commerce_products.uid = product_branch_mm.product_id) AND 
								 (product_branch_mm.branch_id = $branch_id)),
						tx_commerce_products AS products2 LEFT JOIN user_farm_commerce_discount_values ON
								((products.uid = user_farm_commerce_discount_values.product_id) AND 
								 (user_farm_commerce_discount_values.branch_id = $branch_id))
								 
			WHERE		(tx_commerce_products.uid = products2.uid) AND
						tx_commerce_products.uid IN ($products_ids_sql) AND
						($enable_sql)
						
			ORDER BY	tx_commerce_products.uid ASC
						
			";
		
		$moscow_branch = array();
		
		$sql_result = $GLOBALS['TYPO3_DB']->sql_query($sql);
		if ($sql_result) {
			while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($sql_result)) {
				
				$products[$row['product_id']] = $row;
				
				if ((!$auth_user || ($row['branch_price'] == $row['branch_price_opt'] == 0)) && 
					($branch_id != MOSCOW_BRANCH_ID)) {
						
					$moscow_branch[] = $row['product_id'];
					$products[$row['product_id']]['moscow_branch']['branch_price'] = 0;
					$products[$row['product_id']]['moscow_branch']['branch_price_opt'] = 0;
					$products[$row['product_id']]['moscow_branch']['rest'] = 0;
					$products[$row['product_id']]['moscow_branch']['discount'] = 0;
				}
				
			}
		}
		
		if (count($moscow_branch)) {
			$products_ids_sql = implode(',', $moscow_branch);
			$moscow_branch_id = MOSCOW_BRANCH_ID;
			
			$sql = "
			SELECT		product_branch_mm.product_id
						product_branch_mm.branch_price,
						product_branch_mm.branch_price_opt,
						product_branch_mm.rest,
						
						user_farm_commerce_discount_values.discount
			
			FROM		product_branch_mm LEFT JOIN user_farm_commerce_discount_values ON
								(product_branch_mm.product_id = user_farm_commerce_discount_values.product_id)
								 
			WHERE		product_branch_mm.product_id IN ($products_ids_sql) AND
						(product_branch_mm.branch_id = $moscow_branch_id)
						
			ORDER BY	product_branch_mm.product_id ASC
						
			";
			
			$sql_result = $GLOBALS['TYPO3_DB']->sql_query($sql);
			if ($sql_result) {
				while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($sql_result)) {
					
					$products[$row['product_id']]['moscow_branch'] = $row;
				}
			}
		}
		
		
		
		if ($auth_user) {
			
			$sql = "
					SELECT	uid_local AS product_id,
							uid_foreign AS category_id
							
					FROM	tx_commerce_products_categories_mm
					WHERE	uid_local IN ($products_ids_sql)
			";
			$sql_result = $GLOBALS['TYPO3_DB']->sql_query($sql);
			if ($sql_result) {
				while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($sql_result)) {
					$products[$row['product_id']]['category'] = $row['category_id'];
				}
			}
		
			$person_dscnt['products'] = array();
			$person_dscnt['categories'] = array();
			$person_dscnt['manufacturers'] = array();
			
			$enable_sql = tslib_cObj::enableFields('user_farm_commerce_discounts');
			
			$sql = "
					SELECT		discount_type,
								
								products,
								categories,
								manufacturers,

								discount
								
					FROM		user_farm_commerce_discounts
					
					WHERE		($enable_sql) AND
								(branch = -2) AND
								(FIND_IN_SET($uid, users) <> 0)
			";
								
			$sql_result = $GLOBALS['TYPO3_DB']->sql_query($sql);
			
			$person_dscnts = FALSE;
			
			if ($sql_result) {
				while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($sql_result)) {
					
					$person_dscnts = TRUE;
					if ($row['discount_type'] == 1) {
						
						$ids = explode(',', $row['categories']);
						foreach ($ids as $id) {
							if (!isset($person_dscnt['categories'][$id]) ||
								$person_dscnt['categories'][$id] < $row['discount']) {

								$person_dscnt['categories'][$id] = $row['discount'];
							}
						}
					} elseif ($row['discount_type'] == 2) {
						
						$ids = explode(',', $row['products']);
						foreach ($ids as $id) {
							if (!isset($person_dscnt['products'][$id]) ||
								$person_dscnt['products'][$id] < $row['discount']) {

								$person_dscnt['products'][$id] = $row['discount'];
							}
						}
					} elseif ($row['discount_type'] == 3) {
						
						$ids = explode(',', $row['manufacturers']);
						foreach ($ids as $id) {
							if (!isset($person_dscnt['manufacturers'][$id]) ||
								$person_dscnt['manufacturers'][$id] < $row['discount']) {

								$person_dscnt['manufacturers'][$id] = $row['discount'];
							}
						}
					}
				}
			}
			if ($person_dscnts) {
				foreach ($products as $product_id => $product) {
					
					if (array_key_exists($product['category'], $person_dscnt['categories'])) {
						
						if (!isset($products[$product_id]['person_discount']) ||
							$products[$product_id]['person_discount'] < 
									$person_dscnt['categories'][$product['category']]) {
										
							$products[$product_id]['person_discount'] = $person_dscnt['categories'][$product['category']];				
						}
					}
					
					if (array_key_exists($product['product_id'], $person_dscnt['products'])) {
						
						if (!isset($products[$product_id]['person_discount']) ||
							$products[$product_id]['person_discount'] < 
									$person_dscnt['products'][$product['product_id']]) {
										
							$products[$product_id]['person_discount'] = $person_dscnt['products'][$product['product_id']];				
						}
					}
					
					if (array_key_exists($product['manufacturer_uid'], $person_dscnt['manufacturers'])) {
						
						if (!isset($products[$product_id]['person_discount']) ||
							$products[$product_id]['person_discount'] < 
									$person_dscnt['manufacturers'][$product['product_id']]) {
										
							$products[$product_id]['person_discount'] = $person_dscnt['manufacturers'][$product['manufacturer_uid']];				
						}
					}
				}
			}
			
		}
		
		
	}
	
	//------------------------------------------------------------------------
	function get_products($category_id, $branch_id, 
					       $country_id, $manufacturer_id, 
					       $search_text, $select_params = NULL) {
					       	
		$select_params = array('order_by', 'offset', 'rows_max');
		
		
	}
	
	
}

?>